package com.zcs.app.lucky.ui.clip;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.util.AttributeSet;
import android.view.View;

/**
 * 头像裁剪
 *
 * @author ZengCS
 * @since 2014年8月11日
 */
public class ClipSquareView extends View {
    public static final int BORDER_DISTANCE = 100;// 距离屏幕的边距
    private static final float LINE_BORDER_WIDTH = 2f;// 框框宽度
    // private final int LINE_COLOR = Color.WHITE;
    private final int LINE_COLOR = Color.parseColor("#5ECFBA");
    private final int PANEL_COLOR = Color.parseColor("#76000000");
    private Paint mPaint;
    private static boolean isCircle = true;

    public static void setIsCircle(boolean isCircle) {
        ClipSquareView.isCircle = isCircle;
    }

    public ClipSquareView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        mPaint = new Paint();
        if (isCircle) {
            // 初始化橡皮擦
            mPaint.setAlpha(0);
            mPaint.setColor(Color.argb(255, 0, 0, 0));
            //这个属性是设置paint为橡皮擦重中之重
            //这是重点
            //下面这句代码是橡皮擦设置的重点
            mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
            //上面这句代码是橡皮擦设置的重点（重要的事是不是一定要说三遍）
            mPaint.setAntiAlias(true);
            mPaint.setDither(true);
            mPaint.setStyle(Paint.Style.FILL);
            mPaint.setStrokeCap(Paint.Cap.ROUND);
            mPaint.setStrokeJoin(Paint.Join.ROUND);
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        int width = getWidth();
        int height = getHeight();

        boolean isHorizontal = false;
        if (width > height) {
            isHorizontal = true;
        }

        int outLeft = 0;
        int outTop = 0;
        int borderLength = isHorizontal ? (height - BORDER_DISTANCE * 2) : (width - BORDER_DISTANCE * 2);
        int inLeft = isHorizontal ? ((width - borderLength) / 2) : BORDER_DISTANCE;
        int inTop = isHorizontal ? BORDER_DISTANCE : ((height - borderLength) / 2);
        int inRight = isHorizontal ? (inLeft + borderLength) : borderLength + BORDER_DISTANCE;
        int inBottom = isHorizontal ? borderLength + BORDER_DISTANCE : (inTop + borderLength);

        if (isCircle) {
            canvas.drawColor(PANEL_COLOR);
            // 第一步:画阴影
            float cx = width * 0.5f;
            float cy = height * 0.5f;
            float radius = (inRight - inLeft) * 0.5f;
            canvas.drawCircle(cx, cy, radius, mPaint);
        } else {
            mPaint.setColor(PANEL_COLOR);

            canvas.drawRect(outLeft, outTop, width, inTop, mPaint);
            canvas.drawRect(outLeft, inBottom, width, height, mPaint);
            canvas.drawRect(outLeft, inTop, inLeft, inBottom, mPaint);
            canvas.drawRect(inRight, inTop, width, inBottom, mPaint);

            mPaint.setColor(LINE_COLOR);
            mPaint.setStrokeWidth(LINE_BORDER_WIDTH);

            canvas.drawLine(inLeft, inTop, inLeft, inBottom, mPaint);
            canvas.drawLine(inRight, inTop, inRight, inBottom, mPaint);
            canvas.drawLine(inLeft, inTop, inRight, inTop, mPaint);
            canvas.drawLine(inLeft, inBottom, inRight, inBottom, mPaint);
        }
    }
}
